package org.javaforever.gatescore.utils;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FieldSerialComparator;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontVar;

public class FieldUtil {
	public static String generateRequestGetParameterString(FrontField field, FrontVar request){
		String type = field.getFieldType().getTypeName();
		switch (type) {
		case "long": return "Long.parseLong(" + request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "int": return "Integer.parseInt(" + request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "float": return "Float.parseFloat(" + request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "double": return "Double.parseDouble(" + request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "String": return request.getVarName() + ".getParameter(\""+field.getFieldName()+"\")";
		case "boolean": return "Boolean.parseBoolean("+request.getVarName()+ ".getParameter(\""+field.getFieldName()+"\"))";
		case "BigDecimal": return "new BigDecimal("+request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "Timestamp": return "new Timestamp("+request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		case "Date": return "new Date("+request.getVarName() + ".getParameter(\""+field.getFieldName()+"\"))";
		default : return null;
		}
	}
	
	public static Set<FrontField> filterDeniedFields(Set<FrontField> allFields, Set<FrontField> deniedFields) {
		Set<FrontField> results = new TreeSet<FrontField>(new FieldSerialComparator());
		outer: for (FrontField f : allFields) {
			for (FrontField df : deniedFields) {
				String fieldName = f.getFieldName();
				if (fieldName.equals(df.getFieldName())) {
					continue outer;
				}
			}
			results.add(f);
		}
		return results;
	}
	
	public static String genFieldArrayTextQStr(List<FrontField> fields) {
		StringBuilder sb = new StringBuilder();
		for (FrontField f:fields) {
			sb.append("\"").append(f.getText()).append("\", ");
		}
		String retStr = sb.toString();
		if (retStr.contains(",")) return retStr.substring(0,retStr.length()-2);
		else return retStr;
	}
	
	public static String genFieldArrayTextSingleQuotedStr(List<FrontField> fields) {
		StringBuilder sb = new StringBuilder();
		for (FrontField f:fields) {
			sb.append("'").append(f.getText()).append("', ");
		}
		String retStr = sb.toString();
		if (retStr.contains(",")) return retStr.substring(0,retStr.length()-2);
		else return retStr;
	}
	
	public static String genFieldArrayLowerFirstNameWithPrefixStr(List<FrontField> fields, String prefix) {
		StringBuilder sb = new StringBuilder();
		for (FrontField f:fields) {
			sb.append(prefix).append(f.getLowerFirstFieldName()).append(", ");
		}
		String retStr = sb.toString();
		if (retStr.contains(",")) return retStr.substring(0,retStr.length()-2);
		else return retStr;
	}

	public static boolean isNumeric(FrontField f) {
		boolean retVal = false;
		String fieldType = f.getFieldType().getTypeName();
		if ("int".equals(fieldType)||"Integer".equals(fieldType)||"long".equals(fieldType)||
				"Long".equals(fieldType)||"double".equals(fieldType)||"Double".equals(fieldType)||
				"decimal".equals(fieldType)||"Decimal".equals(fieldType)||"BigDecimal".equals(fieldType)||
				"float".equals(fieldType)||"Float".equals(fieldType)||
				"int32".equals(fieldType)||"int64".equals(fieldType)||
				"float32".equals(fieldType)||"float64".equals(fieldType)||
				"i32".equals(fieldType)||"i64".equals(fieldType)||
				"f32".equals(fieldType)||"f64".equals(fieldType)
				) {
			retVal = true;
		}
		return retVal;
	}
	
	public static boolean isInteger(FrontField f) {
		boolean retVal = false;
		String fieldType = f.getFieldType().getTypeName();
		if ("int".equals(fieldType)||"Integer".equals(fieldType)||"long".equals(fieldType)||
				"Long".equals(fieldType)||"int32".equals(fieldType)||"int64".equals(fieldType)||
				"i64".equals(fieldType)||"i32".equals(fieldType)) {
			retVal = true;
		}
		return retVal;
	}
	
	public static boolean isFloat(FrontField f) {
		boolean retVal = false;
		String fieldType = f.getFieldType().getTypeName();
		if ("float".equals(fieldType)||"double".equals(fieldType)||"Float".equals(fieldType)||
				"Double".equals(fieldType)||"float32".equals(fieldType)||"float64".equals(fieldType)||
				"f32".equals(fieldType)||"f64".equals(fieldType)) {
			retVal = true;
		}
		return retVal;
	}
	
	public static boolean isBoolean(FrontField f) {
		boolean retVal = false;
		String fieldType = f.getFieldType().getTypeName();
		if ("boolean".equals(fieldType)||"Boolean".equals(fieldType)||"bool".equals(fieldType)) {
			retVal = true;
		}
		return retVal;
	}
}